package edu.calpoly.android.echoes;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Paint;
import android.util.Log;

public class ShapeDBAdapter {
	
	/*****************/
	/** STATIC DATA **/
	/*****************/
	
	/** Database information **/
	public static final String DATABASE_NAME = "ShapeListDatabase.db";
	public static final int DATABASE_VERSION = 3;
	
	/** Shape General table information **/
	public static final String DATABASE_TABLE_GAME = "tbl_games";
	
	public static final String GAME_KEY_ID = "_id";
	public static final int GAME_COL_ID = 0;
	
	public static final String GAME_KEY_NAME = "game_name";
	public static final int GAME_COL_NAME = GAME_COL_ID + 1;

	/** Shape Database SQL **/
	public static final String GAME_DATABASE_CREATE = "create table " + DATABASE_TABLE_GAME + " (" + 
		GAME_KEY_ID + " integer primary key autoincrement, " + 
		GAME_KEY_NAME	+ " text not null);";
	
	public static final String GAME_DATABASE_DROP = "drop table if exists " + DATABASE_TABLE_GAME;

	
	/** Shape General table information **/
	public static final String DATABASE_TABLE_SHAPE = "tbl_shapes";
	
	public static final String SHAPE_KEY_ID = "_id";
	public static final int SHAPE_COL_ID = 0;
	
	public static final String SHAPE_KEY_TYPE = "shape_type";
	public static final int SHAPE_COL_TYPE = SHAPE_COL_ID + 1;
	
	public static final String SHAPE_KEY_GAME = "shape_game";
	public static final int SHAPE_COL_GAME = SHAPE_COL_TYPE + 1;

	/** Shape Database SQL **/
	public static final String SHAPE_DATABASE_CREATE = "create table " + DATABASE_TABLE_SHAPE + " (" + 
		SHAPE_KEY_ID + " integer primary key autoincrement, " + 
		SHAPE_KEY_TYPE	+ " integer not null, " +
		SHAPE_KEY_GAME + " integer not null, " +
		"FOREIGN KEY(" + SHAPE_KEY_GAME + ") REFERENCES " + 
		DATABASE_TABLE_GAME + "(" + GAME_KEY_ID + "));";
	
	public static final String SHAPE_DATABASE_DROP = "drop table if exists " + DATABASE_TABLE_SHAPE;
	
	
	/** Circle table information **/
	public static final int CIRCLE_MARKER = 0;
	
	public static final String DATABASE_TABLE_CIRCLE = "tbl_circles";
	
	public static final String CIRCLE_KEY_ID = "circle_id";
	public static final int CIRCLE_COL_ID = 0;
	
	public static final String CIRCLE_KREF_ID = "_id";
	public static final int CIRLE_CREF_ID = CIRCLE_COL_ID + 1;
	
	public static final String CIRCLE_KEY_X = "circle_x";
	public static final int CIRCLE_COL_X = CIRLE_CREF_ID + 1;
	
	public static final String CIRCLE_KEY_Y = "circle_y";
	public static final int CIRCLE_COL_Y = CIRCLE_COL_X + 1;
	
	public static final String CIRCLE_KEY_R = "circle_r";
	public static final int CIRCLE_COL_R = CIRCLE_COL_Y + 1;
	
	public static final String CIRCLE_KEY_VX = "circle_vx";
	public static final int CIRCLE_COL_VX = CIRCLE_COL_R + 1;
	
	public static final String CIRCLE_KEY_VY = "circle_vy";
	public static final int CIRCLE_COL_VY = CIRCLE_COL_VX + 1;
	
	public static final String CIRCLE_KEY_DX = "circle_dx";
	public static final int CIRCLE_COL_DX = CIRCLE_COL_VY + 1;
	
	public static final String CIRCLE_KEY_DY = "circle_dy";
	public static final int CIRCLE_COL_DY = CIRCLE_COL_DX + 1;
	
	public static final String CIRCLE_KEY_SCREEN = "circle_screen";
	public static final int CIRCLE_COL_SCREEN = CIRCLE_COL_DY + 1;
	
	public static final String CIRCLE_KEY_PAINT = "circle_paint";
	public static final int CIRCLE_COL_PAINT = CIRCLE_COL_SCREEN + 1;
	
	public static final String CIRCLE_KEY_HIT = "circle_hit";
	public static final int CIRCLE_COL_HIT = CIRCLE_COL_PAINT + 1;
	

	
	/** Circle Database SQL **/
	public static final String CIRCLE_DATABASE_CREATE = "create table " + DATABASE_TABLE_CIRCLE + " (" + 
		CIRCLE_KEY_ID + " integer primary key autoincrement, " + 
		CIRCLE_KREF_ID + " integer not null, " +
		CIRCLE_KEY_X	+ " real not null, " +
		CIRCLE_KEY_Y	+ " real not null, " + 
		CIRCLE_KEY_R	+ " real not null, " + 
		CIRCLE_KEY_VX	+ " real not null, " + 
		CIRCLE_KEY_VY	+ " real not null, " + 
		CIRCLE_KEY_DX	+ " real not null, " + 
		CIRCLE_KEY_DY	+ " real not null, " +
		CIRCLE_KEY_SCREEN + " integer not null, " + 
		CIRCLE_KEY_PAINT	+ " integer not null, " + 
		//CIRCLE_KEY_HIT + " integer not null " +
		"FOREIGN KEY(" + CIRCLE_KREF_ID + ") REFERENCES " + 
		DATABASE_TABLE_SHAPE + "(" + SHAPE_KEY_ID + "));";
	
	public static final String CIRCLE_DATABASE_DROP = "drop table if exists " + DATABASE_TABLE_CIRCLE;
	
	
	/** Line table information **/
	/*public static final int LINE_MARKER = 1;
	
	public static final String DATABASE_TABLE_LINE= "tbl_lines";
	
	public static final String LINE_KEY_ID = "line_id";
	public static final int LINE_COL_ID = 0;
	
	public static final String LINE_KREF_ID = "_id";
	public static final int LINE_CREF_ID = LINE_COL_ID + 1;
	
	public static final String LINE_KEY_SX = "line_sx";
	public static final int LINE_COL_SX = LINE_CREF_ID + 1;
	
	public static final String LINE_KEY_SY = "line_sy";
	public static final int LINE_COL_SY = LINE_COL_SX + 1;
	
	public static final String LINE_KEY_EX = "line_ex";
	public static final int LINE_COL_EX = LINE_COL_SY + 1;
	
	public static final String LINE_KEY_EY = "line_ey";
	public static final int LINE_COL_EY = LINE_COL_EX + 1;
	
	public static final String LINE_KEY_PAINT = "line_paint";
	public static final int LINE_COL_PAINT = LINE_COL_EY + 1;*/
	
	/** Circle Database SQL **/
	/*public static final String LINE_DATABASE_CREATE = "create table " + DATABASE_TABLE_LINE + " (" + 
		LINE_KEY_ID + " integer primary key autoincrement, " + 
		LINE_KREF_ID + " integer not null, " +
		LINE_KEY_SX	+ " real not null, " +
		LINE_KEY_SY	+ " real not null, " + 
		LINE_KEY_EX	+ " real not null, " + 
		LINE_KEY_EY	+ " real not null, " + 
		LINE_KEY_PAINT	+ " integer not null, " +
		"FOREIGN KEY(" + LINE_KREF_ID + ") REFERENCES " + 
		DATABASE_TABLE_SHAPE + "(" + SHAPE_KEY_ID + "));";
	
	public static final String LINE_DATABASE_DROP = "drop table if exists " + DATABASE_TABLE_LINE;*/
	
	/** Rectangle table information **/
	public static final int RECTANGLE_MARKER = 2;
	
	public static final String DATABASE_TABLE_RECTANGLE= "tbl_rectangles";
	
	public static final String RECTANGLE_KEY_ID = "rectangle_id";
	public static final int RECTANGLE_COL_ID = 0;
	
	public static final String RECTANGLE_KREF_ID = "_id";
	public static final int RECTANGLE_CREF_ID = RECTANGLE_COL_ID + 1;
	
	public static final String RECTANGLE_KEY_LEFT = "rectangle_left";
	public static final int RECTANGLE_COL_LEFT = RECTANGLE_CREF_ID + 1;
	
	public static final String RECTANGLE_KEY_TOP = "rectangle_top";
	public static final int RECTANGLE_COL_TOP = RECTANGLE_COL_LEFT + 1;
	
	public static final String RECTANGLE_KEY_RIGHT = "rectangle_right";
	public static final int RECTANGLE_COL_RIGHT = RECTANGLE_COL_TOP + 1;
	
	public static final String RECTANGLE_KEY_BOTTOM = "rectangle_bottom";
	public static final int RECTANGLE_COL_BOTTOM = RECTANGLE_COL_RIGHT + 1;
	
	public static final String RECTANGLE_KEY_SCREEN = "rectangle_screen";
	public static final int RECTANGLE_COL_SCREEN = RECTANGLE_COL_BOTTOM + 1;
	
	public static final String RECTANGLE_KEY_PAINT = "rectangle_paint";
	public static final int RECTANGLE_COL_PAINT = RECTANGLE_COL_SCREEN + 1;
	
	/** Circle Database SQL **/
	public static final String RECTANGLE_DATABASE_CREATE = "create table " + DATABASE_TABLE_RECTANGLE + " (" + 
		RECTANGLE_KEY_ID + " integer primary key autoincrement, " + 
		RECTANGLE_KREF_ID + " integer not null, " +
		RECTANGLE_KEY_LEFT	+ " real not null, " +
		RECTANGLE_KEY_TOP	+ " real not null, " + 
		RECTANGLE_KEY_RIGHT	+ " real not null, " + 
		RECTANGLE_KEY_BOTTOM	+ " real not null, " +
		RECTANGLE_KEY_SCREEN + " integer not null, " +
		RECTANGLE_KEY_PAINT	+ " integer not null, " +
		"FOREIGN KEY(" + RECTANGLE_KREF_ID + ") REFERENCES " + 
		DATABASE_TABLE_SHAPE + "(" + SHAPE_KEY_ID + "));";
	
	public static final String RECTANGLE_DATABASE_DROP = "drop table if exists " + DATABASE_TABLE_RECTANGLE;
	
	/*******************/
	/** INSTANCE DATA **/
	/*******************/
	
	/** Database Instance **/
	private SQLiteDatabase m_db;
		
	/** Database open/upgrade helper **/
	private ShapeDBHelper m_dbHelper;
	
	private static AudioPlayer m_audioPlayer;
	
	private long gameID;
	
	
	/**
	 * Parameterized Constructor
	 * 
	 * @param context 
	 * 			  The application Context in which this JokeDBAdapter is being
	 * 			  used.
	 */
	public ShapeDBAdapter(Context context) {
		// TODO
		m_dbHelper = new ShapeDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
		m_audioPlayer = new AudioPlayer(context);
	}
	
	
	
	/**
	 * Initializes the underlying SQLiteDatabase object for Writable access.
	 */
	public void open() {
		SQLiteDatabase writer = m_dbHelper.getWritableDatabase();
		m_db = writer;
	}
	
	/**
	 * Closes the underlying SQLiteDatabse object.
	 */
	public void close() {
		m_db.close();
	}
	
	public long currGame() {
		/*Log.e("currGame", gameID + "");*/
		return gameID;
	}
	
	public long newGame() {
		ContentValues cv = new ContentValues();
		cv.put(GAME_KEY_NAME, "Blank Save");
		
		gameID =  m_db.insert(DATABASE_TABLE_GAME, null, cv);
		/*Log.e("New Game", gameID + "");*/
		
		return gameID;
	}
	
	public long newGame(String name) {
		ContentValues cv = new ContentValues();
		cv.put(GAME_KEY_NAME, name);
		
		gameID = m_db.insert(DATABASE_TABLE_GAME, null, cv);
		/*Log.e("New Game", gameID + "");*/
		
		return gameID;
	}
	
	public String gameName() {
		String result = "";
		/*Log.e("loading game", gameID + "");*/
		String selection = GAME_KEY_ID + "=" + gameID;
		String[] columnName = {GAME_KEY_NAME};
		
		/*Log.e("gameName", selection);*/
		
		Cursor c = m_db.query(DATABASE_TABLE_GAME, columnName, selection, null, null, null, null);
		/*Log.e("Cursor size", c.getCount() + "");
		Log.e("Column count", c.getColumnCount() + "");*/
		
		
		/*if(c == null || c.getCount() == 0)
			return "";*/
		if(c != null && c.getCount() != 0) {
			c.moveToFirst();
			result = c.getString(0);
		}
		/*Log.e("gameName", result);*/
		c.close();
		return result;
	}
	
	public long loadGame(int load) {
		String selection = GAME_KEY_ID + "=" + load;
		
		String[] columnName = {GAME_KEY_ID, GAME_KEY_NAME};
		Cursor c = m_db.query(DATABASE_TABLE_GAME, columnName, selection, null, null, null, null);
		
		if(c == null || c.getCount() == 0) {
			c.close();
			return -1;
		}
		c.moveToNext();
		long sID = c.getLong(GAME_COL_ID);
		gameID = sID;
		
		c.close();
		
		return gameID;
	}
	
	public boolean renameGame(String s, long gID) {
		ContentValues cv = new ContentValues();
		/*Log.e("rename game", s + " : " + gID);*/
		
		//cv.put(SHAPE_KEY_TYPE, shape.getType());
		cv.put(GAME_KEY_NAME, s);
		
		String selection = GAME_KEY_ID + "=" + gID;
		
		int count = m_db.update(DATABASE_TABLE_GAME, cv, selection, null);
		
		if(count > 0)
			return true;
		return false;
	}
	
	public long removeGame(long gID) {
		String selection = GAME_KEY_ID + "=" + gID;
		
		String[] columnName = {SHAPE_KEY_ID};
		Cursor c = m_db.query(DATABASE_TABLE_SHAPE, columnName, selection, null, null, null, null);
		
		/*Log.e("remove game", c.getCount() + "");*/
		
		/*if(c == null || c.getCount() == 0)
			return -1;*/
		if(c != null && c.getCount() != 0) {
			c.moveToFirst();
			while(!c.isAfterLast()) {
				long sID = c.getLong(SHAPE_COL_ID);
				//long shapeID = (long) sID;
				Boolean result = removeShape(sID);
				/*Log.e("remove game", "result is: " + result.toString());*/
			}
		}
		/*Log.e("remove game", "removing game");*/
		int count = m_db.delete(DATABASE_TABLE_GAME, selection, null);
		if(count < 0) {
			c.close();
			return -1;
		}
		c.close();
		
		return gID;
	}
	
	public ArrayList<String> getSaves() {
		ArrayList<String> saves = new ArrayList<String>();
		String[] columnName = {GAME_KEY_ID, GAME_KEY_NAME};
		Cursor c = m_db.query(DATABASE_TABLE_SHAPE, columnName, null, null, null, null, null);
		
		if(c == null || c.getCount() == 0) {
			c.close();
			return null;
		}
		c.moveToFirst();
		while(!c.isAfterLast()) {
			String s = c.getString(GAME_COL_NAME);
			saves.add(s);
			
			c.moveToNext();
		}
		c.close();
		return saves;
	}
	
	public Cursor getSavesCursor() {
		String[] columnName = {GAME_KEY_ID, GAME_KEY_NAME};
		return m_db.query(DATABASE_TABLE_GAME, columnName, null, null, null, null, null);
	}
	
	public static String getSaveFromCursor(Cursor cursor) {
		if(cursor == null || cursor.getCount() == 0)
			return null;
		
		String s = cursor.getString(GAME_COL_NAME);
		
		return s;
	}
	
	public void updateSave(String s) {
		
	}
	
	/**
	 * Inserts a Joke object into the Database.
	 * 
	 * @param joke
	 * 			  The Joke object which to be persisted in the Database.
	 * 
	 * @return The row ID of the newly inserted row, or -1 if an error 
	 * 		   occurred.
	 */
	public long insertShape(MyDrawableShapes shape) {
		int check = -1;
		long rowID;
		if(shape instanceof MyCircle)
			check = CIRCLE_MARKER;
		//else if(shape instanceof MyLine)
			//check = LINE_MARKER;
		else if(shape instanceof MyRectangle)
			check = RECTANGLE_MARKER;
		else
			Log.e("ShapeDBAdapter", "insertShape not recognized");
		ContentValues cv = new ContentValues();
		
		cv.put(SHAPE_KEY_TYPE, check);
		cv.put(SHAPE_KEY_GAME, gameID);
		
		rowID = m_db.insert(DATABASE_TABLE_SHAPE, null, cv);
		
		if(check == 0) {
			ContentValues circleV = new ContentValues();
			MyCircle circle = (MyCircle) shape;
			
			circleV.put(CIRCLE_KREF_ID, rowID);
			circleV.put(CIRCLE_KEY_X, circle.getX());
			circleV.put(CIRCLE_KEY_Y, circle.getY());
			circleV.put(CIRCLE_KEY_R, circle.getR());
			circleV.put(CIRCLE_KEY_VX, circle.getVel().getX());
			circleV.put(CIRCLE_KEY_VY, circle.getVel().getY());
			circleV.put(CIRCLE_KEY_DX, circle.getDir().getX());
			circleV.put(CIRCLE_KEY_DY, circle.getDir().getY());
			circleV.put(CIRCLE_KEY_SCREEN, circle.getScreenNumber());
			circleV.put(CIRCLE_KEY_PAINT, circle.getPaint().getColor());
			
			m_db.insert(DATABASE_TABLE_CIRCLE, null, circleV);
		}
	/*	else if(check == 1) {
			ContentValues lineV = new ContentValues();
			MyLine line = (MyLine) shape;
			
			lineV.put(LINE_KREF_ID, rowID);
			lineV.put(LINE_KEY_SX, line.getSX());
			lineV.put(LINE_KEY_SY, line.getSY());
			lineV.put(LINE_KEY_EX, line.getEX());
			lineV.put(LINE_KEY_EY, line.getEY());
			lineV.put(LINE_KEY_PAINT, line.getPaint().getColor());
			
			m_db.insert(DATABASE_TABLE_LINE, null, lineV);
		}*/
		else if(check == 2){
			ContentValues rectangleV = new ContentValues();
			MyRectangle rectangle = (MyRectangle) shape;
			
			rectangleV.put(RECTANGLE_KREF_ID, rowID);
			rectangleV.put(RECTANGLE_KEY_LEFT, rectangle.getLeft());
			rectangleV.put(RECTANGLE_KEY_TOP, rectangle.getTop());
			rectangleV.put(RECTANGLE_KEY_RIGHT, rectangle.getRight());
			rectangleV.put(RECTANGLE_KEY_BOTTOM, rectangle.getBottom());
			rectangleV.put(RECTANGLE_KEY_SCREEN, rectangle.getScreenNumber());
			rectangleV.put(RECTANGLE_KEY_PAINT, rectangle.getPaint().getColor());
			
			/*Log.e("Insert Rectangle", rowID + " " + rectangle.getLeft() + " " +  rectangle.getTop() + " " +
					rectangle.getRight() + " " + rectangle.getBottom() + " " + rectangle.getScreenNumber() + " " +
					rectangle.getPaint().getColor());*/
			
			m_db.insert(DATABASE_TABLE_RECTANGLE, null, rectangleV);
		}
		return rowID;
	}
	
	/**
	 * Returns a Cursor containing all Joke rows in the Database that have a 
	 * rating equal to ratingFilter.
	 * 
	 * If ratingFilter is null the returned Cursor should contain all Joke rows
	 * in the Database.
	 * 
	 * @param ratingFilter
	 * 			  A String containing only the rating value. The rating value 
	 * 			  must contain one of Joke.LIKE, Joke.DISLIKE, Joke.UNRATED, or
	 * 			  be null. 
	 * 
	 * @return A Cursor containing rows in the Database Joke Table whose rating
	 * 		   matches ratingFilter, or all rows if ratingFilter is null. 
	 */
	public ArrayList<MyDrawableShapes> getAllShapes() {//String typeFilter) {
		String selection = null;
		ArrayList<MyDrawableShapes> shapes = new ArrayList<MyDrawableShapes>();
		/*if(typeFilter != null)
			selection = SHAPE_COL_TYPE + "=" + typeFilter;
		
		String[] columnName = {SHAPE_KEY_ID, SHAPE_KEY_TYPE};
		return m_db.query(DATABASE_TABLE_SHAPE, columnName, selection, null, null, null, null);*/
		
		shapes.addAll(getAllCircles());
		//shapes.addAll(getAllLines());
		shapes.addAll(getAllRectangles());
		
		return shapes;
	}
	
	
	
	public ArrayList<MyCircle> getAllCircles() {
		ArrayList<MyCircle> circles = new ArrayList<MyCircle>();
		String query = "SELECT c.circle_x, c.circle_y, c.circle_r, c.circle_vx, " +
				"c.circle_vy, c.circle_dx, c.circle_dy, c.circle_screen, c.Circle_paint " +
		"FROM tbl_circles c, tbl_shapes s, tbl_games g " +
		"WHERE c._id=s._id AND s.shape_game=g._id AND g._id=" + gameID;
		
		Cursor cCursor = m_db.rawQuery(query, null);
		/*Log.e("getAllCircles", cCursor.getCount() + " ");
		Log.e("getAllCircles", cCursor.getColumnCount() + " ");
		Log.e("getAllCircles", cCursor.getColumnName(0) + " ");
		Log.e("getAllCircles", cCursor.getColumnName(1) + " ");
		Log.e("getAllCircles", cCursor.getColumnName(2) + " ");
		Log.e("getAllCircles", cCursor.getColumnName(3) + " ");*/
		//Log.e("getAllCircles", cCursor.getColumnName(4) + " ");
		
		cCursor.moveToFirst();
		while(!cCursor.isAfterLast()) {
			float x = cCursor.getFloat(0);
			float y = cCursor.getFloat(1);
			float r = cCursor.getFloat(2);
			float vx = cCursor.getFloat(3);
			float vy = cCursor.getFloat(4);
			float dx = cCursor.getFloat(5);
			float dy = cCursor.getFloat(6);
			int screen = cCursor.getInt(7);
			Paint p = new Paint();
			p.setColor(cCursor.getInt(8));
			MyCircle circle = new MyCircle(x, y, r, new Vector2D(vx, vy), new Vector2D(dx, dy), p, m_audioPlayer, screen);
			
			circles.add(circle);
			
			cCursor.moveToNext();
		}
		cCursor.close();
		return circles;
	}
	
	/*public ArrayList<MyLine> getAllLines() {
		ArrayList<MyLine> lines = new ArrayList<MyLine>();
		String query = "SELECT l.line_sx, l.line_sy, l.line_ex, l.line_ey, l.line_paint " +
		"FROM tbl_lines l, tbl_shapes s " +
		"WHERE l._id=s._id";
		
		Cursor lCursor = m_db.rawQuery(query, null);
		Log.e("getAllLines", lCursor.getCount() + " ");
		Log.e("getAllLines", lCursor.getColumnCount() + " ");
		
		lCursor.moveToFirst();
		while(!lCursor.isAfterLast()) {*/
			/*float sX = lCursor.getFloat(CIRCLE_COL_X);
			float sY = lCursor.getFloat(CIRCLE_COL_Y);
			float eX = lCursor.getFloat(CIRCLE_COL_R);
			float eY = lCursor.getFloat(CIRCLE_COL_R);
			Paint p = new Paint();
			p.setColor(lCursor.getInt(LINE_COL_PAINT));*/
/*			float sX = lCursor.getFloat(0);
			float sY = lCursor.getFloat(1);
			float eX = lCursor.getFloat(2);
			float eY = lCursor.getFloat(3);
			Paint p = new Paint();
			p.setColor(lCursor.getInt(4));
			MyLine line = new MyLine(sX, sY, eX, eY, p);
			
			lines.add(line);
			
			lCursor.moveToNext();
		}
		lCursor.close();
		return lines;
	}*/
	
	public ArrayList<MyRectangle> getAllRectangles() {
		ArrayList<MyRectangle> rectangles = new ArrayList<MyRectangle>();
		String query = "SELECT r.rectangle_left, r.rectangle_top, r.rectangle_right, r.rectangle_bottom, r.rectangle_screen, r.rectangle_paint " +
		"FROM tbl_rectangles r, tbl_shapes s, tbl_games g " +
		"WHERE r._id=s._id AND s.shape_game=g._id AND g._id=" + gameID;
		
		Cursor rCursor = m_db.rawQuery(query, null);


		rCursor.moveToFirst();
		while(!rCursor.isAfterLast()) {
			/*float top = rCursor.getFloat(RECTANGLE_COL_TOP);
			float left = rCursor.getFloat(RECTANGLE_COL_LEFT);
			float right = rCursor.getFloat(RECTANGLE_COL_RIGHT);
			float bottom = rCursor.getFloat(RECTANGLE_COL_BOTTOM);
			Paint p = new Paint();
			int col = rCursor.getInt(RECTANGLE_COL_PAINT);
			p.setColor(col);*/
			float left = rCursor.getFloat(0);
			float top = rCursor.getFloat(1);
			float right = rCursor.getFloat(2);
			float bottom = rCursor.getFloat(3);
			int screen = rCursor.getInt(4);
			Paint p = new Paint();
			int col = rCursor.getInt(5);
			p.setColor(col);
			MyRectangle rectangle = new MyRectangle(left, top, right, bottom, p, col, screen);
			/*Log.e("Insert Rectangle",  left + " " +  top + " " +
					right + " " + bottom + " " + screen + " " +
					p.getColor());*/
			
			rectangles.add(rectangle);
			
			rCursor.moveToNext();
		}
		rCursor.close();
		return rectangles;
	}

	
	/**
	 * Returns a Cursor containing all Joke rows in the Database.
	 * 
	 * @return A Cursor containing all rows in the Database Joke Table.
	 */
	/*public Cursor getAllShapes() {
		return getAllShapes(null);
	}*/
	
	/**
	 * This method constructs a Joke object using the data from a row in the 
	 * Joke Database Table pointed to by the Cursor passed in.
	 * 
	 * If the cursor is null or empty, the method should return null.
	 * 
	 * @param cursor
	 * 			  A Cursor pointing to a row in the Joke Database Table.
	 * 
	 * @return A Joke object containing the data pointed to by the Cursor 
	 * 		   object passed in, or null if the Cursor is empty or null.
	 */
	/*public static ArrayList<MyDrawableShapes> getShapesFromCursor(Cursor cursor, MyDrawableShapes mDS) {
		if(cursor == null || cursor.getCount() == 0)
			return null;
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast()) {
			
			
			cursor.moveToNext();
		}
		
		String s = cursor.getString(SHAPE_COL_TYPE);
		
		
		if(s.equals("MyCircle")) {
			float x = cursor.getFloat(SHAPE_COL_X);
			float y = cursor.getFloat(SHAPE_COL_Y);
			float r = (float) 1.0;
			
			Paint p = new Paint();
			int i = cursor.getInt(SHAPE_COL_PAINT);
			p.setColor(i);
			
			return new MyCircle(x, y, r, p, m_audioPlayer);
		}
		else if(s.equals("MyLine")) {
			float x = cursor.getFloat(SHAPE_COL_X);
			float y = cursor.getFloat(SHAPE_COL_Y);
			
			Paint p = new Paint();
			int i = cursor.getInt(SHAPE_COL_PAINT);
			p.setColor(i);
			
			return new MyLine(x, y, 100, 100, p);
		}
		else if(s.equals("MyRectangle")) {
			float x = cursor.getFloat(SHAPE_COL_X);
			float y = cursor.getFloat(SHAPE_COL_Y);
			
			Paint p = new Paint();
			int i = cursor.getInt(SHAPE_COL_PAINT);
			p.setColor(i);
			
			return new MyRectangle(x, y, 100, 100, p, i);
		}
		cursor.close();
		return null;
	}*/
	
	/**
	 * Retrieves a row from the Joke Database table whose _id matches the "id"
	 * parameter passed in. A Joke is then constructed and initialized using 
	 * the information contained in the Database row.
	 *   
	 * @param id
	 * 			  The id of the Joke to retrieve from the Database. 
	 * 
	 * @return A Joke object containing the data in the Joke Database Table row
	 * 		   whose _id matches the "id" parameter. 
	 */
	/*public Joke getJoke(long id) {
		String selection = JOKE_KEY_ID + "=" + id;
		
		String[] columnName = {JOKE_KEY_ID, JOKE_KEY_TEXT, JOKE_KEY_RATING, JOKE_KEY_AUTHOR};
		Cursor c = m_db.query(DATABASE_TABLE_JOKE, columnName, selection, null, null, null, null);
		
		c.moveToNext();
		
		Joke jk = new Joke(c.getString(JOKE_COL_TEXT), 
				c.getString(JOKE_COL_AUTHOR), 
				c.getInt(JOKE_COL_RATING), 
				c.getLong(JOKE_COL_ID));
		
		return jk;
	}	*/
	
	public MyDrawableShapes getShape(long id) {
		String selection = SHAPE_KEY_ID + "=" + id;
		
		String[] columnName = {SHAPE_KEY_ID, SHAPE_KEY_TYPE, SHAPE_KEY_GAME};
		Cursor c = m_db.query(DATABASE_TABLE_SHAPE, columnName, selection, null, null, null, null);
		
		if(c == null || c.getCount() == 0)
			return null;
		
		c.moveToNext();
		int type = c.getInt(SHAPE_COL_TYPE);
		long sID = c.getLong(SHAPE_COL_ID);
		
		if(type == CIRCLE_MARKER) {
			String query = "SELECT c.circle_x, c.circle_y, c.circle_r, c.Circle_paint " +
			"FROM tbl_circles c, tbl_shapes s " +
			"WHERE c._id=s._id AND s._id=" + sID;
			
			Cursor cCursor = m_db.rawQuery(query, null);
			if(cCursor == null || cCursor.getCount() == 0) {
				cCursor.close();
				return null;
			}
			
			cCursor.moveToNext();
			
			/*float x = cCursor.getFloat(CIRCLE_COL_X);
			float y = cCursor.getFloat(CIRCLE_COL_Y);
			float r = cCursor.getFloat(CIRCLE_COL_R);
			Paint p = new Paint();
			p.setColor(cCursor.getInt(CIRCLE_COL_PAINT));
			MyCircle circle = new MyCircle(x, y, r, p, m_audioPlayer);*/
			
			float x = cCursor.getFloat(0);
			float y = cCursor.getFloat(1);
			float r = cCursor.getFloat(2);
			float vx = cCursor.getFloat(3);
			float vy = cCursor.getFloat(4);
			float dx = cCursor.getFloat(5);
			float dy = cCursor.getFloat(6);
			Paint p = new Paint();
			p.setColor(cCursor.getInt(7));
			MyCircle circle = new MyCircle(x, y, r, new Vector2D(vx, vy), new Vector2D(dx, dy), p, m_audioPlayer);
			
			cCursor.close();
			c.close();
			
			return circle;
		}
		
	/*	else if(type == LINE_MARKER) {
			String query = "SELECT l.line_sx, l.line_sy, l.line_ex, l.line_ey, l.line_paint " +
			"FROM tbl_lines l, tbl_shapes s " +
			"WHERE l._id=s._id AND s._id=" + sID;
			
			Cursor lCursor = m_db.rawQuery(query, null);
			if(lCursor == null || lCursor.getCount() == 0)
				return null;
			
			lCursor.moveToNext();
			
			float sX = lCursor.getFloat(0);
			float sY = lCursor.getFloat(1);
			float eX = lCursor.getFloat(2);
			float eY = lCursor.getFloat(3);
			Paint p = new Paint();
			p.setColor(lCursor.getInt(4));
			MyLine line = new MyLine(sX, sY, eX, eY, p);
			
			return line;
		}*/
		
		else if(type == RECTANGLE_MARKER) {
			String query = "SELECT r.rectangle_left, r.rectangle_top, r.rectangle_right, r.rectangle_bottom, r.rectangle_paint " +
			"FROM tbl_rectangles r, tbl_shapes s " +
			"WHERE r._id=s._id AND s._id=" + sID;
			
			Cursor rCursor = m_db.rawQuery(query, null);
			if(rCursor == null || rCursor.getCount() == 0) {
				rCursor.close();
				return null;
			}
			
			rCursor.moveToNext();
			
			float top = rCursor.getFloat(0);
			float left = rCursor.getFloat(1);
			float right = rCursor.getFloat(2);
			float bottom = rCursor.getFloat(3);
			Paint p = new Paint();
			int col = rCursor.getInt(4);
			p.setColor(col);
			MyRectangle rectangle = new MyRectangle(left, top, right, bottom, p, col);
			
			rCursor.close();
			c.close();
			
			return rectangle;
		}
		return null;
	}
	
	/**
	 * Removes a Joke object from the Database.
	 * 
	 * @param id
	 * 			  The id of the Joke to remove from the Database.
	 * 
	 * @return True if a row in the database with an _id equaling 'id' was 
	 * 		   found and removed; False otherwise. 
	 */
	public boolean removeShape(long id) {
		String selection = SHAPE_KEY_ID + "=" + id;
		
		/*Log.e("removeShape", "removing");*/
		
		String[] columnName = {SHAPE_KEY_ID, SHAPE_KEY_TYPE, SHAPE_KEY_GAME};
		Cursor c = m_db.query(DATABASE_TABLE_SHAPE, columnName, selection, null, null, null, null);
		
		/*Log.e("remove shape", c.getCount() + "");*/
		
		/*if(c == null || c.getCount() == 0)
			return false;*/
		
		if(c != null && c.getCount() != 0)
		{
			/*Log.e("removeShape", "I'm on the inside man!");*/
			c.moveToFirst();
			//c.moveToNext();
			
			long sID = c.getLong(0);
			int type = c.getInt(1);
			c.close();
			
			
			
			if(type == CIRCLE_MARKER) {
				String cSelection = CIRCLE_KREF_ID + "=" + sID;
				
				int count = m_db.delete(DATABASE_TABLE_CIRCLE, cSelection, null);
				if(count < 0)
					return false;
				
				int fCount = m_db.delete(DATABASE_TABLE_SHAPE, selection, null);
				if(fCount > 0) 
					return true;
				
				return false;
			}
			
			
			else if(type == RECTANGLE_MARKER) {
				String rSelection = RECTANGLE_KREF_ID + "=" + sID;
				
				int count = m_db.delete(DATABASE_TABLE_RECTANGLE, rSelection, null);
				if(count < 0)
					return false;
				
				int fCount = m_db.delete(DATABASE_TABLE_SHAPE, selection, null);
				if(fCount > 0) {
					return true;
				}
				return false;
			}
		}
		c.close();
		return false;
	}
	
	/**
	 * Updates the contents of a Joke in the Database. Will write the data 
	 * contained in the Joke passed in back to the Database. 
	 * 
	 * @param joke
	 * 			  The Joke containing data to write back to the database.
	 * 
	 * @return True if a row in the database with an _id equaling Joke.getID()
	 * 		   was found and updated; False otherwise. 
	 * 
	 */
	/*public boolean updateShape(MyDrawableShapes shape) {
		ContentValues cv = new ContentValues();
		
		//cv.put(SHAPE_KEY_TYPE, shape.getType());
		cv.put(SHAPE_KEY_PAINT, shape.getPaint().toString());
		cv.put(SHAPE_KEY_SPEED, shape.getSpeed().toString());
		cv.put(SHAPE_KEY_X, shape.getX());
		cv.put(SHAPE_KEY_Y, shape.getY());
		
		//String selection = SHAPE_KEY_ID + "=" + shape.getID();
		String selection = "";
		
		int count = m_db.update(DATABASE_TABLE_SHAPE, cv, selection, null);
		
		if(count > 0)
			return true;
		return false;
	}*/
	
	
	/**
	 * Helper class used to wrap the logic necessary for Creating and Upgrading
	 * the underlying SQLiteDatabase that JokeDBAdapter relies on.
	 */
	private static class ShapeDBHelper extends SQLiteOpenHelper {

		/**
		 * Parameterized constructor. Should only call its super version of 
		 * this constructor, passing in all the parameters it is given.
		 */
		public ShapeDBHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}
		

		/**
		 * Called when the Database is created for the first time.
		 */
		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(ShapeDBAdapter.GAME_DATABASE_CREATE);
			_db.execSQL(ShapeDBAdapter.SHAPE_DATABASE_CREATE);
			//Log.e("onCreate", CIRCLE_DATABASE_CREATE);
			_db.execSQL(ShapeDBAdapter.CIRCLE_DATABASE_CREATE);
			//_db.execSQL(ShapeDBAdapter.LINE_DATABASE_CREATE);
			_db.execSQL(ShapeDBAdapter.RECTANGLE_DATABASE_CREATE);
		}

		/**
		 * Called when the Constructor is called with a version number that 
		 * differs from the current Database version.
		 * 
		 * This method is used to perform upgrades to the existing database. 
		 * If you should have to upgrade the database, this is where you could
		 * save data currently in the database before migrating to the new 
		 * schema. 
		 */
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
			_db.execSQL(ShapeDBAdapter.GAME_DATABASE_DROP);
			_db.execSQL(ShapeDBAdapter.SHAPE_DATABASE_DROP);
			_db.execSQL(ShapeDBAdapter.CIRCLE_DATABASE_DROP);
			//_db.execSQL(ShapeDBAdapter.LINE_DATABASE_DROP);
			_db.execSQL(ShapeDBAdapter.RECTANGLE_DATABASE_DROP);
			onCreate(_db);
		}

	}

}
